let Register={
	getCaptcha:function(){
		var time = 10;
		const element = PF('captcha_btn').jq;
		element.attr("disabled","disabled");
		element.addClass("ui-state-disabled");
		element.find("span").text(time + "s");
		const interval = window.setInterval(()=>{
			if(time -- <=0){
				time = "resend";
				element.removeAttr("disabled");
				element.removeClass("ui-state-disabled");
				window.clearInterval(interval);
			}
			element.find("span").text(time + "s");
		},1000)
		sendCaptcha();
    }
}
let Role={
	selectId:'',
	getCheckboxGroup:function(callback){
		var groups = new Map();
		var key = "";
		PF('permission').inputs.each(function(index,item){
			if(item.id.indexOf(key)==-1 || key == ''){
				key = item.id;
				groups.set(key,new Array());
			}
			groups.get(key).push({'input':item,'isChecked':$(PF('permission').outputs[index]).hasClass('ui-state-active') ,'div':PF('permission').outputs[index]});
		})
		
		groups.forEach(function(value,key){
			callback(value,key);
		})
		
		return groups;
	}	,
	init:function(){
		Role.getCheckboxGroup(function(value,key){
			var checkCount = 0;
			var all = value.shift();
			value.forEach(function(v,k){
				if(v.isChecked){
					checkCount++;
				}
			})
			if(all.isChecked){
				if(checkCount < value.length){
					$(all.div).find('span').removeClass('ui-icon-check');
					$(all.div).find('span').addClass('pi pi-minus');
				}else if(checkCount == value.length){
					$(all.div).find('span').removeClass('pi pi-minus');
					$(all.div).find('span').addClass('ui-icon-check');
				}
			}
		});
	},
		
	onSelectAll:function(elemId){
		var group = [];
		PF('permission').inputs.filter('[id*='+elemId+']').each(function(index,item){
			var i = PF('permission').inputs.index(item);
			group.push({'input':item,'isChecked':$(PF('permission').outputs[i]).hasClass('ui-state-active') ,'div':PF('permission').outputs[i]});
		})
		var all = group.shift();
		group.forEach(function(value,key){
			if(!Role.selectId){
				if(all.isChecked? value.isChecked : !value.isChecked){
					$(value.input).click();
				}
			}
		})
		if(all.isChecked){
			$(all.div).find('span').removeClass('pi pi-minus');
		}
	},
	onSelectOne:function(elemId){
		var allId = elemId.slice(0,elemId.lastIndexOf('_'));
		Role.selectId = elemId;
		var group = [];
		var currElem = "";
		PF('permission').inputs.filter('[id*='+allId+']').each(function(index,item){
			var i = PF('permission').inputs.index(item);
			var elem = {'input':item,'isChecked':$(PF('permission').outputs[i]).hasClass('ui-state-active') ,'div':PF('permission').outputs[i]}
			if(elemId == item.id){
				currElem=elem
			}
			group.push(elem);
		})
		var all = group.shift();
		if(!all.isChecked){
			$(all.input).click();
			$(all.div).find('span').removeClass('ui-icon-check');
			$(all.div).find('span').addClass('pi pi-minus');
		}
		var checkCount = 0;
		group.forEach(function(v,k){
			if(v.isChecked){
				checkCount++;
			}
		})
		console.log(checkCount);
		
		
		
		if (checkCount==group.length-1){
			if(currElem.isChecked){
				$(all.div).find('span').removeClass('ui-icon-check');
				$(all.div).find('span').addClass('pi pi-minus');
			}else{
				$(all.div).find('span').removeClass('pi pi-minus');
				$(all.div).find('span').addClass('ui-icon-check');
			}
		}else if(checkCount==group.length){
			$(all.div).find('span').removeClass('ui-icon-check');
			$(all.div).find('span').addClass('pi pi-minus');
		}else if(checkCount == 1){
			if(currElem.isChecked){
				$(all.input).click();
			}
		}
		Role.selectId = "";
	}
	
}
let Avatar={
	cropper : '',
	upload:function(e){
		var image = document.getElementById('image');
		var files = e.target.files;
		var reader;
        var file;
     
		if (files && files.length > 0) {
      		file = files[0];
      	}
      	
      	var done = function (url) {
          PF('upload_input').jq.val('');
          image.src = url;
          $('.upload-panel').hide();
          $('.crop-panel').show();
          
         this.cropper = new Cropper(image, {
              aspectRatio: 1,
              viewMode: 1,
              preview:'.preview',
              movable:false
           });
        };
        
      	if (URL) {
        	done(URL.createObjectURL(file));
        } else if (FileReader) {
        	reader = new FileReader();
        	reader.onload = function (e) {
          	done(reader.result);
        };
        reader.readAsDataURL(file);
      }
	},
	
	save:function(){
		 if (cropper) {
	          canvas = cropper.getCroppedCanvas({
	            width: 160,
	            height: 160,
	          });
	          
	          canvas.toBlob(function (blob) {
	        	  var reader = new FileReader();
	        	  reader.readAsDataURL(blob); 
	        	  reader.onloadend = function() {
	        	      base64data = reader.result;                
	        	      saveAvatar([{name:'avatar', value:base64data}]);
	        	  }
	          })
		 }
	},
	
	reset:function(){
		if(cropper){
			cropper.reset();
		}
	},
	scaleX:function(){
		if(cropper){
			var value = cropper.getData('scale').scaleX*-1;
			cropper.scaleX(value);
			
		}
	},
	scaleY:function(){
		if(cropper){
			var value = cropper.getData('scale').scaleY*-1;
			cropper.scaleY(value);
		}
	}
	
}